﻿Imports Microsoft.VisualBasic

Namespace WebApi
    Namespace Instagram

        Public Class InstagramClient

            Private webClient As WebApi.Net.WebClient

            Public Sub New(oauth2 As WebApi.Oauth.Oauth2)

                Me.webClient = New WebApi.Net.WebClient
                Me.webClient.oAuth2 = oauth2

            End Sub

            Public Property LastError As Net.ServiceError

            Private Function Download(address As String) As String

                Dim res = Encoding.UTF8.GetString(Me.webClient.DownloadData(address))

                webClient.postParams.Clear()

                If Me.webClient.StatusCode <> 200 Then

                    Me.LastError = New Net.ServiceError With {
                        .webClient = Me.webClient,
                        .ResError = res,
                        .Net = WebApi.Type.INSTAGRAM
                    }

                    Select Case Me.webClient.StatusCode
                        Case 500, 502, 503, 504
                            'Internal Server Error
                            'Bad Gateway
                            'Service Unavailable
                            'Gateway timeout

                            'Me.LastError = New Net.ServiceError With {.Message = Me.webClient.StatusCodeDescr, .Code = Me.webClient.StatusCode}
                            Me.LastError.Code = Me.webClient.StatusCode
                            Me.LastError.Message = Me.webClient.StatusCodeDescr

                            Net.ExceptionBase.ThrowException(New Net.ExceptionConnectionServer With {.ServiceError = Me.LastError})

                        Case Else

                            Try
                                Dim Err As ServiceErrors
                                Err = MyApp.JSONDeserializeObj(Of ServiceErrors)(res)

                                Me.LastError.Code = Err.error.code
                                Me.LastError.Message = Err.error.message

                            Catch ex As Exception

                                Me.LastError.Code = Me.webClient.StatusCode
                                Me.LastError.Message = res

                            End Try


                            Select Case Me.webClient.StatusCode

                                Case 400
                                    'Unauthorized
                                    Select Case Me.LastError.Code
                                        Case 190
                                            '190 not authorized application
                                            Net.ExceptionBase.ThrowException(New Net.ExceptionAPIAccount With {.ServiceError = Me.LastError})

                                    End Select

                            End Select




                    End Select

                    Net.ExceptionBase.ThrowException(New Net.ExceptionBase With {.ServiceError = Me.LastError})

                End If


                Return res

            End Function

            'Private Sub ThrowException(ex As Net.ExceptionServiceBase)

            '    ex.webclient = Me.webClient
            '    ex.ServiceError = Me.LastError

            '    Dim WebError = SF("client:{0}, {1} - FACEBOOK API ERROR: {2}", Me.webClient.StatusCode, Me.webClient.StatusCodeDescr, Me.LastError.ResError)

            '    ex.Data.Add("ServiceResponse_Error", WebError)
            '    ex.Data.Add("ServiceResponse_URL", Me.webClient.ResponseUri.AbsolutePath)
            '    ex.Data.Add("ServiceResponse_oauthToken", Me.webClient.oAuth2.access_token)

            '    UI.Page.WriteLog(WebError)

            '    Throw ex
            '    'Return ex

            'End Sub
            Public Class ServiceErrors

                Public Property [error] As ServiceError

            End Class
            Public Class ServiceError

                Public Property message As String
                Public Property code As Integer
                Public Property error_subcode As Integer
                Public Property type As String


            End Class

#Region "USER FUNCTIONS"

            Public Function GetCurrUser() As User

                Return Me.GetUsersLookup("self")

            End Function
            Public Function GetUsersLookup(user_id As String) As User

                Dim Uri As New UriBuilder(SF("https://api.instagram.com/v1/users/{0}", user_id))

                Dim queryString = HttpUtility.ParseQueryString("")

                'queryString.Add("fields", String.Join(",", fields))

                'If screen_name <> "" Then
                '    queryString.Add("screen_name", screen_name)
                'Else
                '    queryString.Add("user_id", user_id)
                'End If

                Uri.Query = queryString.ToString


                Dim Res = Me.Download(Uri.Uri.AbsoluteUri)
                'Return Res
                Return MyApp.JSONDeserializeObj(Of Result(Of User))(Res).data


            End Function

#End Region









        End Class

        Public Class Result(Of tres)

            Public Property data As tres

            Public Property meta As ResultMeta

            Public Property pagination As ResultPaging

        End Class


        Public Class ResultPaging

            Public Property next_url As String
            Public Property next_max_id As String

        End Class
        Public Class ResultMeta

            Public Property error_type As String
            Public Property code As Integer
            Public Property error_message As String

        End Class

        Public Class User



            Public Property id As String
            Public Property username As String

            Public Property full_name As String
            Public Property profile_picture As String
            Public Property bio As String
            Public Property website As String
            Public Property counts As UserCounts


            'Public Shared Function DefFieldsLookup() As List(Of String)

            '    Return New List(Of String) From {
            '        "name", "link", "username", "location",
            '        "picture", "accounts"
            '    }

            'End Function

            'accounts

        End Class

        Public Class UserCounts

            Public Property media As Integer
            Public Property follows As Integer
            Public Property followed_by As Integer

        End Class



    End Namespace
End Namespace